package com.ibeeking.found.file.service.common.dos;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;

import com.ibeeking.nematos.file.oss.operation.upload.domain.UploadFileResult;

import lombok.Data;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 文件实体类
 *
 * @author ma116
 */
@Data
@TableName("file")
public class FileDO implements Serializable {
    private static final long serialVersionUID = -1L;

    @TableId(type = IdType.ASSIGN_ID)
    private Long fileId;

    /**
     * 文件url
     */
    @TableField(value = "file_id")
    private String fileUrl;

    /**
     * 文件大小
     */
    @TableField(value = "file_size")
    private Long fileSize;

    /**
     * 文件状态(0-失效，1-生效)
     */
    @TableField(value = "file_status")
    private Integer fileStatus;

    /**
     * 存储类型
     */
    @TableField(value = "storage_type")
    private Integer storageType;

    /**
     * md5唯一标识
     */
    @TableField(value = "identifier")
    private String identifier;

    /**
     * 创建时间
     */
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField(value = "create_time")
    private LocalDateTime createTime;

    /**
     * 创建用户id
     */
    @TableField(value = "create_by")
    private Long createBy;

    /**
     * 修改时间
     */
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField(value = "modify_time")
    private LocalDateTime modifyTime;

    /**
     * 修改用户id
     */
    @TableField(value = "modify_by")
    private Long modifyBy;

    public FileDO(){

    }

    public FileDO(UploadFileResult uploadFileResult) {
        this.fileId = IdUtil.getSnowflakeNextId();
        this.fileUrl = uploadFileResult.getFileUrl();
        this.fileSize = uploadFileResult.getFileSize();
        this.fileStatus = 1;
        this.storageType = uploadFileResult.getStorageType().getCode();
        this.identifier = uploadFileResult.getIdentifier();
        this.createTime = LocalDateTime.now();

    }

    public FileDO(String fileUrl, Long fileSize, Integer storageType, String identifier, Long userId) {
        this.fileId = IdUtil.getSnowflakeNextId();
        this.fileUrl = fileUrl;
        this.fileSize = fileSize;
        this.fileStatus = 1;
        this.storageType = storageType;
        this.identifier = identifier;
        this.createTime = LocalDateTime.now();
        this.createBy = userId;
    }

}
